﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;

using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Cookie : System.Web.UI.Page
{
    private string uid;   //用户名
    private string pwd;   //密码
    public int exp_time;  // Cookie生存期
    Random r = new Random();//声明一个随机数作为URL地址后面的随机参数。

    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie myCookie = new HttpCookie("someCookie");
        HttpCookie Cookie = Request.Cookies["someCookie "];
        Label1.Visible = false;
        if (!IsPostBack)
        {
            if (Cookie != null) //如果本页Cookie对象不为空，则向下进行
            {
                if (Cookie.Values["auth"] == "admin")
                {
                    Response.Write("你已登陆系统");
                    Panel1.Visible = false;
                    Label1.Visible = true;
                }
            }
            else
            {
                Panel1.Visible = true;
            }
        }
        else
        {
            uid = Request.Form["txtName"];//uid变量的值为txtName输入框的值
            pwd = Request.Form["txtPwd"];//pwd变量的值为txtPwd输入框的值
            if (uid == "lau" && pwd == "abcdefg")
            {
                string exp = ddlcookie.SelectedItem.Value;
                switch (exp)
                {
                    case "no": exp_time = 0; break;
                    case "oneday": exp_time = 1; break;
                    case "oneweek": exp_time = 7; break;
                    case "onemonth": exp_time = 30; break;
                    default: exp_time = 0; break;
                }
                myCookie.Values.Add("auth", "admin");
                //myCookie这个集合填加一个名为“auth”，值为“admin”的 Cookie
                if (exp_time != 0)
                {
                    myCookie.Expires = DateTime.Now.AddDays(exp_time);
                }
                Response.Cookies.Add(myCookie);
                Response.Redirect("Cookie.aspx?rand=" + r.Next(10000));
            }

        }
    }
}